package com.clover.framework.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class RegexUtils {
	public static void main(String args[]) {
		//System.out.println(RegexUtil.ereg("^\\d+$", "5432432t"));
		/*System.out.println(RegexUtil.ereg_replace("[a-z]+", "me", "fdafda"));
		List<String> list = RegexUtil.regMatchAll2List("<(.*?)href=['\"](?<href>(.*?))['\"](.*?)>", "<a href='http://www.baidu.com'><a href='http://www.cnblog.com'>", "href");
		if (list != null && list.size() > 0) {
			for (String s : list) {
				System.out.println(s);
			}
		}
		list = RegexUtil.regMatchAll2List("<(.*?)href=['\"](?<href>(.*?))['\"](.*?)>", "<a href='http://www.baidu.com'><a href='http://www.cnblog.com'>", "href");
		if (list != null && list.size() > 0) {
			for (String s : list) {
				System.out.println(s);
			}
		}*/
		
		
		String str = "<img border=\"0\" src=\"/cloud_backweb/ewebeditor/sysimage/icon16/zip.gif\"><a href=\"/cloud_backweb/ewebeditor/uploadfile/20160902174030413.zip\" target=\"_blank\">域名地址.zip</a>";
		
		List<String[]> list = regMatchGroupList("<a(.*?)href=\"(.*?)\".*?>(.*?\\.(rar|zip|txt|doc|docx|pdf|xls|xlsx|ppt|pptx))</a>",str,new int[]{2,3,4});
		for (String[] arr : list) {
			
			System.out.println(arr[0]);
			System.out.println(arr[1]);
			System.out.println(arr[2]);
		}

		str = "<img border=\"0\" src=\"/cloud_backweb/ewebeditor/sysimage/icon16/zip.gif\"/><img border=\"0\" src=\"/cloud_backweb/ewebeditor/sysimage/icon16/zip.png\"/>";
		List<String> srcs = regMatchAll2List("<img(.*?)src=['\"]?(?<src>.*?)['\"]", str, "src");
		for (String src : srcs) {

			System.out.println(src);
		}
	}

	/**
	 * 是否与与正则表达式相匹配 
	 * 
	 * @param pattern
	 * @param str
	 * @return
	 * @throws PatternSyntaxException
	 */
	public static final boolean ereg(String pattern, String str) throws PatternSyntaxException {
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(str);
			return m.find();
		} catch (PatternSyntaxException e) {
			throw e;
		}
	}

	/**
	 * 匹配且替换字串
	 * 
	 * @param pattern 正则表达式模式
	 * @param newstr 要替换匹配到的新字串
	 * @param str 原始字串
	 * @return 匹配后的字符串
	 * @since 1.0
	 */
	public static final String replace(String pattern, String newstr, String str) throws PatternSyntaxException {
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(str);
			return m.replaceAll(newstr);
		} catch (PatternSyntaxException e) {
			throw e;
		}
	}

	/**
	 * 主要用于模板中模块标记分析函数 把查找到的元素加到vector中
	 * 
	 * @param pattern 为正则表达式模式
	 * @param str
	 *            原始字串
	 * @return vector
	 * @since 1.0
	 */
	public static final Vector splitTags2Vector(String pattern, String str) throws PatternSyntaxException {
		Vector vector = new Vector();
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(str);
			while (m.find()) {
				vector.add(replace("(\\[\\#)|(\\#\\])", "", m.group()));
			}
			return vector;
		} catch (PatternSyntaxException e) {
			throw e;
		}
	}

	/**
	 * 模块标记分析函数 功能主要是把查找到的元素加到vector中
	 * 
	 * @param pattern 为正则表达式模式
	 * @param str
	 *            原始字串
	 * @since 1.0
	 */
	public static final String[] splitTags(String pattern, String str) {
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(str);
			String[] array = new String[m.groupCount()];
			int i = 0;
			while (m.find()) {
				array[i] = replace("(\\[\\#)|(\\#\\])", "", m.group());
				i++;
			}
			return array;
		} catch (PatternSyntaxException e) {
			throw e;
		}
	}

	/**
	 * 匹配所有符合模式要求的字串并加到矢量vector数组中
	 * 
	 * @param pattern 为正则表达式模式
	 * @param str
	 *            原始字串
	 * @return vector
	 * @since 1.0
	 */
	public static final Vector regMatchAll2Vector(String pattern, String str) throws PatternSyntaxException {
		Vector vector = new Vector();
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(str);
			while (m.find()) {
				vector.add(m.group());
			}
			return vector;
		} catch (PatternSyntaxException e) {
			throw e;
		}
	}

	/**
	 * 匹配所有符合模式要求的字串并加到字符串数组中
	 * 
	 * @param pattern 为正则表达式模式
	 * @param str
	 *            原始字串
	 * @param groupName
	 *            分组名称，可以为空
	 * @return array
	 * @since 1.0
	 */
	public static final List<String> regMatchAll2List(String pattern, String str, String groupName) throws PatternSyntaxException {
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(str);
			List<String> list = new ArrayList<String>();
			String s = null;
			while (m.find()) {
				if (!groupName.isEmpty()) {
					s = m.group(groupName);
					if (null != s && s.length() > 0)
						list.add(s);
				} else {
					list.add(m.group());
				}
			}
			return list;
		} catch (PatternSyntaxException e) {
			throw e;
		}
	}

	/**
	 * 匹配第一个匹配的字串
	 * 
	 * @param pattern 为正则表达式模式
	 * @param str 原始字串
	 * @param groupIndex 分组
	 * @return array
	 * @since 1.0
	 */
	public static final String regMatchGroup(String pattern, String str, Integer groupIndex) throws PatternSyntaxException {
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(str);
			@SuppressWarnings("unused")
			String s = null;
			while (m.find()) {
				if (groupIndex == null) {
					return m.toString();
				} else {
					return m.group(groupIndex);
				}
			}
		} catch (PatternSyntaxException e) {
			throw e;
		}
		return "";
	}
	
	/**
	 * @Title: regMatchGroupList 
	 * @Description: 
	 * @author: masl 2016年9月6日下午1:31:48
	 * @param @param pattern
	 * @param @param str
	 * @param @param groupArr
	 * @param @return
	 * @param @throws PatternSyntaxException 
	 * @return List<String[]>
	 * @throws
	 */
	public static final List<String[]> regMatchGroupList(String pattern, String str, int[] groupArr) throws PatternSyntaxException {
		List<String[]> list =new ArrayList<String[]>();
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(str);
			while (m.find()) {
				String[] arr = new String[groupArr.length];
				int i = 0;
				for (Integer index : groupArr) {
					arr[i] = m.group(index);
					i++;
				}
				list.add(arr);
			}
		} catch (PatternSyntaxException e) {
			throw e;
		}
		return list;
	}
	/**
	 * 转义正则表达式字符(之所以需要将\和$字符用escapeDollarBackslash方法的方式是因为用repalceAll是不行的，简单的试试
	 * "$".repalceAll("\\$","\\\\$")你会发现这个调用会导致数组越界错误)
	 * 
	 * @param original 原始字串
	 * @return array
	 * @since 1.0
	 */
	public static String escapeDollarBackslash(String original) {
		StringBuffer buffer = new StringBuffer(original.length());
		for (int i = 0; i < original.length(); i++) {
			char c = original.charAt(i);
			if (c == '\\' || c == '$') {
				buffer.append("\\").append(c);
			} else {
				buffer.append(c);
			}
		}
		return buffer.toString();
	}

	/**
	 * @Description: 提取指定字串的函数 功能主要是把查找到的元素
	 * @author: masl --- 2016-8-2上午11:13:55
	 * @param @param pattern
	 * @param @param str
	 * @param @return 
	 * @return String
	 * @throws
	 */
	public static final String fetchStr(String pattern, String str) {
		String returnValue = null;
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(str);
			while (m.find()) {
				returnValue = m.group();
			}
			return returnValue;
		} catch (PatternSyntaxException e) {
			return returnValue;
		}
	}
}
